package com.qxsx.midi.activity.study;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.activity.exam.CityData;
import com.qxsx.midi.adapter.CityAdapter;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.model.City;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.widget.ContactItemInterface;
import com.qxsx.midi.widget.ContactListViewImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by Zrj on 2015/9/8.
 */
public class StudyLocalCityListActivity extends BaseActivity implements TextWatcher {
    private Context context_ = StudyLocalCityListActivity.this;
    private ContactListViewImpl listview;
    private EditText searchBox;
    private String searchString;
    private TextView tvselected,tv_now;
    private Object searchLock = new Object();
    boolean inSearchMode = false;
    private final static String TAG = "MainActivity2";

    List<ContactItemInterface> contactList;
    List<ContactItemInterface> filterList;
    private SearchListTask curSearchTask = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_list);
        setTitle("选择城市");
        setNavBtn(R.mipmap.back, 0);
        filterList = new ArrayList<ContactItemInterface>();
        contactList = CityData.getSampleContactList();
        CityAdapter adapter = new CityAdapter(this, R.layout.city_list_item, contactList);
        listview = (ContactListViewImpl) this.findViewById(R.id.listview);
        listview.setFastScrollEnabled(true);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View v, int position,
                                    long id) {
                List<ContactItemInterface> searchList = inSearchMode ? filterList
                        : contactList;

                SPUtils.put(AppContext.getContext(), "chooes_city_id", searchList.get(position).getCityidInfo().toString());
                SPUtils.put(AppContext.getContext(), "chooes_city_name", searchList.get(position).getDisplayInfo());
                finish();
            }
        });
        searchBox = (EditText) findViewById(R.id.input_search_query);
        searchBox.addTextChangedListener(this);
        tvselected = (TextView)findViewById(R.id.tv_selected);
        String city = (String) SPUtils.get(AppContext.getContext(), "chooes_city_name", "全国");
        tvselected.setText(city);
        tv_now = (TextView) findViewById(R.id.tv_now);
        String city_name = (String) SPUtils.get(AppContext.getContext(), "city_name","");
        tv_now.setText(city_name);
        //getPos(this);
    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {

        switch (component) {
            case LEFT:
                finish();

                break;
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
        searchString = searchBox.getText().toString().trim().toUpperCase();

        if (curSearchTask != null
                && curSearchTask.getStatus() != AsyncTask.Status.FINISHED) {
            try {
                curSearchTask.cancel(true);
            } catch (Exception e) {
                Log.i(TAG, "Fail to cancel running search task");
            }

        }
        curSearchTask = new SearchListTask();
        curSearchTask.execute(searchString);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // do nothing
    }

    private class SearchListTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            filterList.clear();
            String keyword = params[0];
            inSearchMode = (keyword.length() > 0);
            if (inSearchMode) {
                // get all the items matching this
                for (ContactItemInterface item : contactList) {
                    City contact = (City) item;
                    boolean isPinyin = contact.getFullName().toUpperCase().indexOf(keyword) > -1;
                    boolean isChinese = contact.getNickName().indexOf(keyword) > -1;
                    if (isPinyin || isChinese) {
                        filterList.add(item);
                    }
                }
            }
            return null;
        }

        protected void onPostExecute(String result) {
            synchronized (searchLock) {
                if (inSearchMode) {
                    CityAdapter adapter = new CityAdapter(context_, R.layout.city_list_item, filterList);
                    adapter.setInSearchMode(true);
                    listview.setInSearchMode(true);
                    listview.setAdapter(adapter);
                } else {
                    CityAdapter adapter = new CityAdapter(context_, R.layout.city_list_item, contactList);
                    adapter.setInSearchMode(false);
                    listview.setInSearchMode(false);
                    listview.setAdapter(adapter);
                }
            }
        }
    }
}
